package com.lure.base.statemachine.builder;

import com.lure.base.statemachine.State;
import com.lure.base.statemachine.StateMachine;
import com.lure.base.statemachine.StateMachineFactory;
import com.lure.base.statemachine.impl.StateMachineImpl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yanwei li create  2024-05-29 00:03
 */
public class StateMachineBuilderImpl<S,E,C> implements StateMachineBuilder<S,E,C>{

    private final Map<S, State<S,E,C>> stateMap = new ConcurrentHashMap<>();

    private final StateMachineImpl<S,E,C> stateMachine = new StateMachineImpl<>(stateMap);

    @Override
    public TransitionBuilder<S, E, C> addTransition() {
        TransitionBuilderImpl<S,E,C> transitionBuilder = new TransitionBuilderImpl<>(stateMap);
        return transitionBuilder;
    }

    @Override
    public StateMachine<S, E, C> build(String machineId) {
        stateMachine.setMachineId(machineId);
        StateMachineFactory.register(stateMachine);
        return stateMachine;
    }

}
